package model

import (
	"api/database"
	"api/utils/logger"
	"github.com/jinzhu/gorm"
)

/**
 * @Description
 * @Author huan.wang
 * @Date 2021/5/7 1:15 下午
 **/

type DeviceSecretModel struct {
	BaseModel
	gorm.Model
	DID    uint   `gorm:"column:did"`
	Secret string `gorm:"column:secret"`
}

func NewDeviceSecretModel() *DeviceSecretModel {
	return &DeviceSecretModel{}
}

func (DeviceSecretModel) TableName() string {
	return "device_secret"
}

func (dsm DeviceSecretModel) Insert() (interface{}, error) {
	if err := database.GetDB().Model(&dsm).Create(&dsm).Error; err != nil {
		logger.Error.Println("[Model] insert error", err)
		return nil, err
	}
	var id []uint
	database.GetDB().Raw("select LAST_INSERT_ID() as id").Pluck("id", &id)
	return id[0], nil
}

func (dsm DeviceSecretModel) Select() (deviceSecret DeviceSecretModel) {

	database.GetDB().Model(&dsm).Where("did = ?", dsm.DID).First(&deviceSecret)
	return
}
